package merchant

import (
	"com.cyt.yunpay/app/model"
	"com.cyt.yunpay/app/service/pay"
	"com.cyt.yunpay/library/response"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

// 商户API管理对象
var Merchant = new(merchantApi)

type merchantApi struct{}

// 查询保留资金余额
func (a *merchantApi) GetCheckAccountFile(r *ghttp.Request) {
	var (
		apiReq *model.GetCheckAccountFileReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"Date":     apiReq.Date,
		"FileType": apiReq.FileType,
	}
	data, err := pay.CommonPay("allinpay", "MerchantGetCheckAccountFile", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 查询保留资金余额
func (a *merchantApi) QueryReserveFundBalance(r *ghttp.Request) {
	params := g.Map{
	}
	data, err := pay.CommonPay("allinpay", "MerchantQueryReserveFundBalance", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 会员子账号汇款入金上账通知
func (a *merchantApi) NotifySubAcctReserveFundIn(r *ghttp.Request) {
	var (
		apiReq *model.NotifySubAcctReserveFundInReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":       apiReq.Username,
		"AcctOrgType":     apiReq.AcctOrgType,
		"SubAcctNo":       apiReq.SubAcctNo,
		"SubAcctName":     apiReq.SubAcctName,
		"AccountSetNo":    apiReq.AccountSetNo,
		"Trxid":           apiReq.Trxid,
		"OrderNo":         apiReq.OrderNo,
		"Status":          apiReq.Status,
		"ExAccountNo":     apiReq.ExAccountNo,
		"ExAccountName":   apiReq.ExAccountName,
		"ExUnionBank":     apiReq.ExUnionBank,
		"ExUnionBankName": apiReq.ExUnionBankName,
		"Amount":          apiReq.Amount,
		"FundInTime":      apiReq.FundInTime,
		"Summary":         apiReq.Summary,
	}
	data, err := pay.CommonPay("allinpay", "MerchantNotifySubAcctReserveFundIn", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 查询商家余额
func (a *merchantApi) QueryMerchantBalance(r *ghttp.Request) {
	var (
		apiReq *model.QueryMerchantBalanceReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"AccountSetNo": apiReq.AccountSetNo,
	}
	data, err := pay.CommonPay("allinpay", "MerchantQueryMerchantBalance", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 查询银行卡余额
func (a *merchantApi) QueryBankBalance(r *ghttp.Request) {
	var (
		apiReq *model.QueryBankBalanceReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"AcctOrgType": apiReq.AcctOrgType,
		"AcctNo":      apiReq.AcctNo,
		"AcctName":    apiReq.AcctName,
	}
	data, err := pay.CommonPay("allinpay", "MerchantQueryBankBalance", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 平台银行存管账户余额查询
func (a *merchantApi) QueryBankInExpDetail(r *ghttp.Request) {
	var (
		apiReq *model.QueryBankInExpDetailReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"AcctOrgType":   apiReq.AcctOrgType,
		"StartDate":     apiReq.StartDate,
		"EndDate":       apiReq.EndDate,
		"IsFirstFlag":   apiReq.IsFirstFlag,
		"StartPosition": apiReq.StartPosition,
		"QueryNum":      apiReq.QueryNum,
	}
	data, err := pay.CommonPay("allinpay", "MerchantQueryBankInExpDetail", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 电子回单下载
func (a *merchantApi) EleReceiptDownload(r *ghttp.Request) {
	var (
		apiReq *model.EleReceiptDownloadReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId": apiReq.Username,
	}
	data, err := pay.CommonPay("allinpay", "MerchantEleReceiptDownload", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}
